package com.zzc.qqchat;


import com.zzc.qqchat.domain.Friends;
import com.zzc.qqchat.repository.FriendsRep;
import com.zzc.qqchat.domain.UserInfor;
import com.zzc.qqchat.repository.UserInforRep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Optional;

@SpringBootApplication
@EnableScheduling
public class Test01Application {

    public static void main(String[] args) {
        SpringApplication.run(Test01Application.class, args);

    }

    @Bean
    public CommandLineRunner demo(UserInforRep userInforRep, FriendsRep friendsRep){
        return (args) ->{
            UserInfor user1 = new UserInfor();
            user1.setUserId(1);
            user1.setIsOnline(0);
            user1.setUserName("用户1");
            user1.setPassword("111");
            user1.setPicture("http://img.wxcha.com/file/201810/23/5e623a6c2f.jpeg");
            userInforRep.save(user1);

            user1.setUserId(2);
            user1.setIsOnline(0);
            user1.setUserName("用户2");
            user1.setPassword("222");
            user1.setPicture("http://down.52pk.com/uploads/190929/5038_101837_4491.jpg");
            userInforRep.save(user1);

            user1.setUserId(3);
            user1.setIsOnline(0);
            user1.setUserName("用户3");
            user1.setPassword("333");
            user1.setPicture("http://m.imeitou.com/uploads/allimg/181221/3-1Q2211A444.jpg");
            userInforRep.save(user1);

            user1.setUserId(4);
            user1.setIsOnline(0);
            user1.setUserName("用户4");
            user1.setPassword("444");
            user1.setPicture("http://zhangzhic.gitee.io/zhangzhicblog/img/avatar.png");
            userInforRep.save(user1);

            Optional<Friends> have = friendsRep.findById(0);
            if(!have.isPresent()){
                Friends friends=new Friends();
                friends.setId(0);
                friends.setUserId(1);
                friends.setFriendId(2);
                friendsRep.save(friends);

                friends.setId(1);
                friends.setUserId(1);
                friends.setFriendId(3);
                friendsRep.save(friends);

                friends.setId(2);
                friends.setUserId(2);
                friends.setFriendId(1);
                friendsRep.save(friends);

                friends.setId(3);
                friends.setUserId(2);
                friends.setFriendId(3);
                friendsRep.save(friends);

                friends.setId(4);
                friends.setUserId(3);
                friends.setFriendId(1);
                friendsRep.save(friends);

                friends.setId(5);
                friends.setUserId(3);
                friends.setFriendId(2);
                friendsRep.save(friends);
            }


        };
    }

}
@Configuration
class WebMvcConfiguration implements WebMvcConfigurer {
    private final Logger logger = LoggerFactory.getLogger(WebMvcConfiguration.class);

    public void addResourceHandlers(ResourceHandlerRegistry registry){
        logger.info("add resourceHandlers");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
    
}

